﻿Function Get-EventHandler
{
  [OutputType('EventHandler[]')]
  [CmdletBinding(DefaultParameterSetName="FriendlyName")]
  param
  (
    [parameter(Position=0,
               ParameterSetName="Number")]
    [int]
    $Number = -1,

    [parameter(Position=0,
               ParameterSetName="FriendlyName")]
    [ValidateNotNullOrEmpty()]
    [String]
    $FriendlyName,

    [parameter(Position=0,
               ParameterSetName="Id")]
    [System.Guid]
    $Id
  )

  Begin
  {
  }

  Process
  {
    try
    {
      $DeserializedHandlerList = Import-Clixml $Script:HandlerXMLPath
    }
    catch [System.IO.FileNotFoundException]
    {
      # If the handlers.xml file doesn't exist, create an empty array.
      $DeserializedHandlerList = @()
    }

    # Import-Clixml returns deserialized objects, which need to be converted to normal ones.
    $HandlerList = @()
    foreach ($DeserializedHandler in $DeserializedHandlerList)
    {
      switch ($DeserializedHandler.Category.ToString())
      {
        "File"
        {
          $Handler = New-Object EventHandlerFile
          $Handler.LogPath = $DeserializedHandler.LogPath
        }
        "EventLog"
        {
          $Handler = New-Object EventHandlerEventLog
          $Handler.LogName = $DeserializedHandler.LogName
        }
        "Console"
        {
          $Handler = New-Object EventHandlerConsole
        }
      }
      $Handler.Number                  = $DeserializedHandler.Number
      $Handler.Id                      = $DeserializedHandler.Id
      $Handler.FriendlyName            = $DeserializedHandler.FriendlyName
      $Handler.Category                = $DeserializedHandler.Category
      $Handler.LogEntryFormat          = $DeserializedHandler.LogEntryFormat
      $Handler.TimeStampFormat         = $DeserializedHandler.TimeStampFormat
      $Handler.SourceReplacementRegex  = $DeserializedHandler.SourceReplacementRegex
      $Handler.MessageReplacementRegex = $DeserializedHandler.MessageReplacementRegex
      $Handler.DetailsReplacementRegex = $DeserializedHandler.DetailsReplacementRegex

      $HandlerList += $Handler
    }

    # Filter the returned handlers by number, length, or Id.  Throw an exception if none are found.
    if ($Number -ge 0)
    {
      $HandlerList = $($HandlerList | Where-Object {$_.Number -eq $Number})
      if ($HandlerList -eq $Null)
      {
        $Exception = New-Object System.ArgumentException "A parameter is invalid.  Cannot find an Event Handler with number $Number."
        throw $Exception
      }
    }
    if ($FriendlyName.Length -gt 0)
    {
      $HandlerList = $($HandlerList | Where-Object {$_.FriendlyName -eq $FriendlyName})
      if ($HandlerList -eq $Null)
      {
        $Exception = New-Object System.ArgumentException "A parameter is invalid.  Cannot find an Event Handler with Friendly Name '$FriendlyName'"
        throw $Exception
      }
    }
    if ($Id -ne $Null)
    {
      $HandlerList = $($HandlerList | Where-Object {$_.Id -eq $Id})
      if ($HandlerList -eq $Null)
      {
        $Exception = New-Object System.ArgumentException "A parameter is invalid.  Cannot find an Event Handler with Id '$Id'"
        throw $Exception
      }
    }

    $HandlerList = $HandlerList | Sort-Object Number

    return $HandlerList
  }

  End
  {
  }
}